#-----------------------------------------------------------------------------
#
#    setup.py
#
#    Installation script
#
#    Copyright (c) 2011 Evolui, LLC <evolui1@evolui.org>
#
#    SOFTWARE NOTICE AND LICENSE
#
#    This file is part of py-openzwave-extension.
#
#    py-openzwave-extension is free software: you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of the License,
#    or (at your option) any later version.
#
#    py-openzwave-extension is distributed in the hope that it will be useful, 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with py-openzwave-extension.  If not, see
#    <http://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------
import os
from distutils.core import setup, Extension

# replace the two variables below with hard paths if desired.
pyopenzwave_root = os.path.abspath(os.path.dirname(__file__))
# equates to ../openzwave/cpp.
openzwave_root = os.path.join(os.path.dirname(pyopenzwave_root), "openzwave", "cpp")

if os.name == "nt":
    #
    # Makes assumption that SetEnv.cmd parent is in system path:
    #  usually C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\
    #
    # it is required to run this command for 64-bit windows as it will set
    # variables that query_vcvarsall looks for in msvc9compiler.py.  for
    # some reason on 32-bit machines the vcvarsall.bat script updates the env
    # and then outputs the new env which python parses.  for 64-bit, it only
    # outputs the current environment, and doesn't update anything.  this
    # line does the updating.
    #
    # unfortunately, executing this as part of this script is too late.  so
    # this needs to be done from the command line prior to executing this
    # script
    #
    #os.system("SetEnv.cmd /Release")

    import distutils.msvc9compiler
    #
    # HACK to get python compiled with 9.0 to use 10.0 windows sdk for extensions
    # because microsoft doesn't easily provide 9.0 sdk anymore.
    #
    distutils.msvc9compiler.VERSION = 10.0
    #
    # auto included by setup.py.  if you attempt to include it yourself, it
    # will cause obscure errors.
    #
    #python_include_dir = ''# 'C:\\Python31\\include' 
    libraries = ["setupapi"] # standard windows
    hid_extension = "hid.cpp"
elif os.name == "posix":
    libraries = ["udev"] # sudo apt-get install libudev-dev
    hid_extension = "hid.c"

def expand(path, extension = '.cpp'):
    matches = []
    filepaths = os.listdir(path)
    for filepath in filepaths:
        if filepath.endswith(extension):
            matches.append(os.path.join(path, filepath))
    
    return matches

def update_path_based_on_os(path):
    paths = path
    if type(paths) is not list:
        paths = [paths]
        
    newpaths = []
    for path in paths:
        # put the appropriate slashes in the path
        newpaths.append(os.path.normpath(path))

    if len(newpaths) == 1:
        return newpaths[0]    
    else:
        return newpaths

def get_platform_name():
    if os.name == "posix":
        return "unix"
    elif os.name == "nt":
        return "windows"

def get_platform_name2():
    if os.name == "posix":
        return "linux"
    elif os.name == "nt":
        return "windows"

openzwave_root = update_path_based_on_os(openzwave_root)

sources = [os.path.join(pyopenzwave_root, 'src/pyopenzwave.cpp')]
sources.extend(expand(os.path.join(openzwave_root, 'src')))
sources.extend(expand(os.path.join(openzwave_root, 'src/command_classes')))
sources.extend(expand(os.path.join(openzwave_root, 'tinyxml')))
sources.extend(expand(os.path.join(openzwave_root, 'src/value_classes')))
sources.extend(expand(os.path.join(openzwave_root, 'src/platform')))
sources.extend(expand(os.path.join(openzwave_root, 'src/platform/' + get_platform_name())))
sources.extend(expand(os.path.join(openzwave_root, 'hidapi/' + get_platform_name2()), extension = hid_extension))
sources = update_path_based_on_os(sources)
#print(sources)
#import sys
#sys.exit(0)
include_dirs = [#python_include_dir, # not sure why i ever needed this? should be automatic.
                os.path.join(openzwave_root, 'src'),
                os.path.join(openzwave_root, 'src/command_classes'),
                os.path.join(openzwave_root, 'src/value_classes'),
                os.path.join(openzwave_root, 'src/platform'),
                os.path.join(openzwave_root, 'hidapi/hidapi'),
                os.path.join(openzwave_root, 'src/platform/' + get_platform_name()),
                os.path.join(openzwave_root, 'tinyxml'),
                ]
include_dirs = update_path_based_on_os(include_dirs)

pyopenzwave_module = Extension('pyopenzwave',
                               define_macros = [('MAJOR_VERSION', '0'),
                                                ('MINOR_VERSION', '1')],
                               include_dirs = include_dirs,
                               libraries = libraries,
                               #library_dirs = ['/usr/local/lib'],
                               sources = sources,
                               )

setup (name = 'pyopenzwave',
       version = '0.1',
       description = 'Python extension wrapping OpenZWave',
       author = 'Evolui, LLC',
       author_email = 'evolui1<at>evolui<dot>org',
       url = 'www<dot>evolui<dot>org',
       ext_modules = [pyopenzwave_module])

